package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.domain.Patient;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * This class loads a gui screen for adding patients to the database.
 * 
 * @author Lee
 */
public class AddPatientPanel extends AbstractPanelLayout
{
	private static final long serialVersionUID = 1L;
	private JButton back;
	private Container container;
	private CardLayout c1;
	private JTextField lastNameField;
	private JTextField firstNameField;
	private JTextField middleNameField;
	private JTextField addressField;
	private JTextField citystateField;
	private JTextField zipField;
	private JTextField telephoneField;
	private JTextField cellField;
	private JTextField emailField;
	private JTextField pharmacyField;
	private JTextField insuranceField;
	private JTextField allergiesField;
	private JButton submit;

	private Map<String, Object> patientMap;
	private JTextField username;
	private JTextField password;
	private JTextField doctorField;
	private Object logdumy;

	/**
	 * Constructor for the addpatient panel that performs what is stated 
	 * in the class description. 
	 * 
	 * @param c1
	 * @param container
	 */
	public AddPatientPanel(final CardLayout c1, final Container container)
	{
		numCenterPanels = 1;
		this.c1 = c1;
		this.container = container;
		initialize();
		MainWindow.welcomeMessage.setText("Create Patient");

		panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.Y_AXIS));
		JPanel subLeft = new JPanel();

		subLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblPersonalInformation = new JLabel("1.  Personal Information");
		subLeft.add(lblPersonalInformation, "4, 2");

		JLabel lblLastName = new JLabel("Last Name");
		subLeft.add(lblLastName, "4, 4");

		JLabel lblFirstName = new JLabel("First Name");
		subLeft.add(lblFirstName, "6, 4");

		JLabel lblMiddle = new JLabel("Middle");
		subLeft.add(lblMiddle, "8, 4");

		lastNameField = new JTextField();
		subLeft.add(lastNameField, "4, 6, fill, default");
		lastNameField.setColumns(10);

		firstNameField = new JTextField();
		subLeft.add(firstNameField, "6, 6, fill, default");
		firstNameField.setColumns(10);

		middleNameField = new JTextField();
		subLeft.add(middleNameField, "8, 6, fill, default");
		middleNameField.setColumns(10);

		JLabel lblAddress = new JLabel("Address");
		subLeft.add(lblAddress, "4, 8");

		JLabel lblCitystate = new JLabel("City/State");
		subLeft.add(lblCitystate, "6, 8");

		JLabel lblZip = new JLabel("Zip");
		subLeft.add(lblZip, "8, 8");

		addressField = new JTextField();
		subLeft.add(addressField, "4, 10, fill, default");
		addressField.setColumns(10);

		citystateField = new JTextField();
		subLeft.add(citystateField, "6, 10, fill, default");
		citystateField.setColumns(10);

		zipField = new JTextField();
		subLeft.add(zipField, "8, 10, fill, default");
		zipField.setColumns(10);

		JLabel lblTelephoneHome = new JLabel("Telephone - Home");
		subLeft.add(lblTelephoneHome, "4, 12");

		JLabel lblCell = new JLabel("Cell");
		subLeft.add(lblCell, "6, 12");

		JLabel lblEmailAddress = new JLabel("Email Address");
		subLeft.add(lblEmailAddress, "8, 12");

		telephoneField = new JTextField();
		subLeft.add(telephoneField, "4, 14, fill, default");
		telephoneField.setColumns(10);

		cellField = new JTextField();
		subLeft.add(cellField, "6, 14, fill, default");
		cellField.setColumns(10);

		emailField = new JTextField();
		subLeft.add(emailField, "8, 14, fill, default");
		emailField.setColumns(10);

		JLabel lblMedicalInformation = new JLabel("2.  Medical Information");
		subLeft.add(lblMedicalInformation, "4, 16");

		JLabel lblPharmacy = new JLabel("Pharmacy");
		subLeft.add(lblPharmacy, "4, 18");

		JLabel lblInsuranceCarrier = new JLabel("Insurance Carrier");
		subLeft.add(lblInsuranceCarrier, "6, 18");

		back = new JButton("Back");
		back.addActionListener(new ActionListener() 
		{
			/**
			 * This method moves the user back to the admin panel.
			 * 
			 * @param event The ActionEvent object handling the listening.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
				MainWindow.welcomeMessage.setText("Welcome");
			}
		});


		panelLeft.add(subLeft);
		pharmacyField = new JTextField();
		subLeft.add(pharmacyField, "4, 20, fill, default");
		pharmacyField.setColumns(10);

		insuranceField = new JTextField();
		subLeft.add(insuranceField, "6, 20, fill, default");
		insuranceField.setColumns(10);

		JLabel lblAllergiesPlease = new JLabel("Allergies - Please list");
		subLeft.add(lblAllergiesPlease, "4, 22");

		JLabel lblDoctor = new JLabel("Doctor");
		subLeft.add(lblDoctor, "6, 22");

		allergiesField = new JTextField();
		subLeft.add(allergiesField, "4, 24, fill, default");
		allergiesField.setColumns(10);

		doctorField = new JTextField();
		subLeft.add(doctorField, "6, 24, fill, default");
		doctorField.setColumns(10);

		JLabel lblUsernamepassword_1 = new JLabel("3.  Username/password");
		subLeft.add(lblUsernamepassword_1, "4, 26");

		JLabel lblUsername = new JLabel("Username");
		subLeft.add(lblUsername, "4, 28");

		JLabel lblPassword = new JLabel("Password");
		subLeft.add(lblPassword, "6, 28");

		username = new JTextField();
		subLeft.add(username, "4, 30, fill, default");
		username.setColumns(10);

		password = new JTextField();
		subLeft.add(password, "6, 30, fill, default");

		submit = new JButton("Submit");
		ButtonListener listener = new ButtonListener();
		submit.addActionListener(listener);
		JPanel subLeft2 = new JPanel();
		subLeft2.add(submit, "4, 28, 2, 1");
		subLeft2.add(back);
		panelLeft.add(subLeft2);
	}

	/**
	 * Private listener class that handles all of the widgets on the 
	 * add patient panel screen.
	 * 
	 * @author Lee
	 */
	private class ButtonListener implements ActionListener
	{
		/**
		 * The method that needs to be implemented for the action listener
		 * interface.
		 * 
		 * @param e The action event handling the button listening.
		 */
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == submit)
			{
				if (!(lastNameField.getText().equals("") || firstNameField.getText().equals("") || 
						middleNameField.getText().equals("") || addressField.getText().equals("") ||
						citystateField.getText().equals("") || zipField.getText().equals("") || 
						telephoneField.getText().equals("") || cellField.getText().equals("") ||
						emailField.getText().equals("") || pharmacyField.getText().equals("") || 
						insuranceField.getText().equals("") || allergiesField.getText().equals("") ||
						username.getText().equals("") || password.getText().equals("") || 
						doctorField.getText().equals("")))
				{
					String lastName = lastNameField.getText();
					String firstName = firstNameField.getText();
					String middleName = middleNameField.getText();
					String address = addressField.getText();
					String city = citystateField.getText();
					String zip = zipField.getText();
					String telephone = telephoneField.getText();
					String cell = cellField.getText();
					String email = emailField.getText();
					String pharmacy = pharmacyField.getText();
					String insurance = insuranceField.getText();
					String allergies= allergiesField.getText();
					String user = username.getText();
					String pass = password.getText();
					String doctor = doctorField.getText();

					patientMap = new HashMap<String, Object>();
					Map<String, String> personalInfo = new HashMap<String, String>();
					personalInfo.put("last name", lastName);
					personalInfo.put("first name", firstName);
					personalInfo.put("middle name", middleName);
					personalInfo.put("address", address);
					personalInfo.put("city", city);
					personalInfo.put("zip", zip);
					personalInfo.put("telephone", telephone);
					personalInfo.put("cell", cell);
					personalInfo.put("email", email);

//					patientMap.put("patient", patient);
//					patientMap.put("requestType", "createPatient");
//					JOptionPane.showMessageDialog(null, "Patient created");

					Patient patient = new Patient(user, pass, personalInfo, null, pharmacy, 
							insurance, allergies);
					patient.setUsername(user);
					patient.setPassword(pass);
					HibernateDriver driver = new HibernateDriver();
					driver.SaveObject(patient);
					patientMap.put("patient", patient);
					patientMap.put("requestType", "createPatient");
					JOptionPane.showMessageDialog(null, "Patient created");

				}
				else JOptionPane.showMessageDialog(null, "Please fill out all forms");
			}
		}
	}
}
